package com.lanou.musicbox.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;

import com.lanou.musicbox.activity.MainActivity;
import com.lanou.musicbox.bean.Mp3Info;
import com.lanou.musicbox.fragment.IMusicBackService;

import java.io.IOException;
import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MusicService extends Service {
    // 定义一个播放对象
    private MediaPlayer mMediaPlayer;
    private List<Mp3Info> mList = null;

    private int mNowPlayIndex = 0;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        //setDataSource();
        return mBinder;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        mMediaPlayer = new MediaPlayer();
       // mList = MusicListAdapter.mList;
        super.onCreate();
    }
    @SuppressLint("NewApi") @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        if(mMediaPlayer.isPlaying()){
            sendReceiverUpdateUI();
        }else{
            setDataSource();
        }
        return super.onStartCommand(intent, flags, startId);
    }
    // 设置资源
    void setDataSource(){
        try {
            mMediaPlayer.reset(); // 重新实例化
            mMediaPlayer.setOnPreparedListener(null);
          //  mMediaPlayer.setDataSource(mList.get(mNowPlayIndex).mMusicPath); //设置需要加载的文件
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // 设置音频流
            mMediaPlayer.prepare();  //储卡中内容加载或者网络中的部分媒体内容加载到内存中

        } catch (IOException ex) {
            // TODO: notify the user why the file couldn't be opened

            return;
        } catch (IllegalArgumentException ex) {
            // TODO: notify the user why the file couldn't be opened

            return;
        }

        mMediaPlayer.setOnCompletionListener(mMusicCompletionListener);
        sendReceiverUpdateUI();
    }

    MediaPlayer.OnCompletionListener mMusicCompletionListener=new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {
            // TODO Auto-generated method stub
            playNextMusic();
        }

    };
    /**
     * 播放下一曲歌曲
     */
    private void playNextMusic(){
        if(mNowPlayIndex+1>mList.size()-1){
            mNowPlayIndex = 0;
        }
        else{
            mNowPlayIndex++;
        }

        setDataSource();

        mMediaPlayer.start();
    }

    private void sendReceiverUpdateUI(){
        Intent i = new Intent(MainActivity.UPDATE_UI);
        sendBroadcast(i);
    }
    /**
     * 实现aidl的接口
     */
    private final IMusicBackService.Stub mBinder = new IMusicBackService.Stub(){

        // 返回当前播放到位置
        @Override
        public int getNowPlayingMusicCurTime() throws RemoteException {
            // TODO Auto-generated method stub
            return mMediaPlayer.getCurrentPosition();
        }

        // 返回当前音频长度
        @Override
        public int getNowPlayingMusicMaxTime() throws RemoteException {
            // TODO Auto-generated method stub
            return mMediaPlayer.getDuration();
        }


        //
        @Override
        public boolean getMusicIsPlaying() throws RemoteException {
            // TODO Auto-generated method stub
            return mMediaPlayer.isPlaying();
        }

        // 暂停播放
        @Override
        public void stopMusic() throws RemoteException {
            // TODO Auto-generated method stub
            System.out.println("start");
            mMediaPlayer.pause();
        }

        // 开始播放
        @Override
        public void playMusic() throws RemoteException {
            // TODO Auto-generated method stub
            System.out.println("start");
            mMediaPlayer.start();
        }

        @Override
        public void playUpMusic() throws RemoteException {
            // TODO Auto-generated method stub
            if(mNowPlayIndex-1<0){
                mNowPlayIndex = mList.size()-1;
            }else{
                mNowPlayIndex--;
            }
            setDataSource();
            mMediaPlayer.start();
        }

        // 播放下一首
        @Override
        public void PlayNextMusic() throws RemoteException {
            // TODO Auto-generated method stub
            playNextMusic();
        }

        @Override
        public void setNowPlay(int time) throws RemoteException {
            // TODO Auto-generated method stub
            mNowPlayIndex = time;
        }

        @Override
        public int getNowPlay() throws RemoteException {
            // TODO Auto-generated method stub
            return mNowPlayIndex;
        }

        @Override
        public void setDataSourceAndPlay() throws RemoteException {
            // TODO Auto-generated method stub
            setDataSource();
            mMediaPlayer.start();
        }

        @Override
        public void setProgress(int progress) throws RemoteException {
            // TODO Auto-generated method stub
            mMediaPlayer.seekTo(progress);
        }


    };

}
